package org.kev.transit.worker;

import java.util.ArrayList;
import org.kev.transit.data.Route;
import android.app.Activity;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.util.Log;

public class RouteUpdateWorker implements Runnable{
	
	private static final String TAG = null;
	private Activity mParent=null;;
	private ArrayList<Route> mList=null;
	public RouteUpdateWorker(Activity parent, ArrayList <Route> list){
		mParent=parent;
		mList=list;
	}
	public void run(){
		int count = mList.size();
		String where =null;		
		Cursor cursor = null;
		
		for(int i=0;i<count;i++){
			Route data = mList.get(i);
			//uri.addId(Long.parseLong(s.id) );
			where = Route.ROUTE_ID + "=? AND "+  
					Route.ROUTE_TYPE + "=? " ;				
			//Log.d(TAG,"wheres="+where);
			String args[]={data.id,data.routeType};
			if(data.id==null|| data.routeType==null){
				Log.e(TAG,"Null Parms! ="+data.id+data.routeType);
				return;
			}
			try{
				cursor = mParent.managedQuery(Route.CONTENT_URI, new String[] 
				     {Route.ROUTE_ID},where,args,null);
				//update lookup table
				Log.d(TAG,"found in db #"+cursor.count());
				
				if(cursor.count()==0){
					insertData(data);
				}else{
					updateData(data);
				}
			}catch(Exception e){
				Log.e(TAG,"UNKNOWN error will be ignored",e);
			}finally{
				if(cursor!=null)
					cursor.close();
			}

		}		
		
	}
	/**
	 * insert data object into DB table
	 * @param data
	 * @return row URI
	 */
	private Uri insertData(Route data){
		Log.d(TAG,">>> insertRoute"+mParent+data);
		if (mParent ==null || data==null){
			Log.e(TAG,"null parms returing");
			return null;
		}

		Log.d(TAG, "+++insertRoute");
		Log.d(TAG, data.toXml() );
		ContentValues values = new ContentValues();
		values.put(Route.ROUTE_ID, data.id);
		values.put(Route.ROUTE_TYPE, data.routeType);
		values.put(Route.TITLE, data.title);
		values.put(Route.CREATE_DATE,new  Long(System.currentTimeMillis() ) );
		
		Uri curl = mParent.getContentResolver().insert(Route.CONTENT_URI,values);
		//done with lookup now work on timeTable
			Log.d(TAG,"<<< insertRoute"+curl);

		return curl;
	}

	/**
	 * update date in DB with data object
	 * @param data
	 * @return row count
	 */
	private int updateData(Route data){
		if (mParent ==null|| data==null){
			Log.e(TAG,"null parms returing");
			return 0;
		}
		Log.d(TAG, "+++updateRoute");
		Log.d(TAG, data.toXml() );
		ContentValues values = new ContentValues();
		values.put(Route.ROUTE_ID, data.id);
		values.put(Route.ROUTE_TYPE, data.routeType);
		values.put(Route.TITLE, data.title);
		values.put(Route.CREATE_DATE,new  Long(System.currentTimeMillis() ) );
		
		String where = Route.ROUTE_ID+"=? AND "+Route.ROUTE_TYPE+"=? ";
						
		String[] args={data.id,data.routeType};

		int row = mParent.getContentResolver().update(Route.CONTENT_URI,values,where,args);
		Log.d(TAG,"row="+row );
		return row;
	}

}

